<?php
// 说明:2015-07-06  by abc777
// 1.先获取账号的供应商列表，获取的产品必须在这些供应商中，已发现BUG：现供销关系断开还能购买还能转分销。
// 2.获取直销中的产品权限 pft_product_sale_list。
// 3.获取转分销中的产品   pft_p_apply_evolute 
// 4.

include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/class/abc/Product.class.php';
include_once '/var/www/html/new/d/class/abc/PFTCore777.class.php';
include_once '/var/www/html/new/d/common/area.inc.php';


if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"登陆超时","code":0}');


$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$memberSID = $sid = $_SESSION['sid'];// 会员ID

$prods = $suppliers = $dnames = $sales = $lids = $lands = $saleAll_aid = array();

$start = microtime(true);

// 获取供应商列表
$suppliers = $suppliers_t = PFTCore777::Member_Suppliers($memberSID);
// $suppliers = unset($suppliers[$_SESSION['sid']]);
// 按供应商搜索
if($_REQUEST['supplier']){
	$key = array_search($_REQUEST['supplier'], $suppliers);
	if($key===false) exit(json_encode(array('lists' => array(), 'total' => 0, 'code'=>200)));
	$suppliers = array($key=>$_REQUEST['supplier']);
}

if(count($suppliers)==0) exit(json_encode(array('lists' => array(), 'total' => 0, 'code'=>200)));

$suppliers_siz = count($suppliers);
$suppliers_str = implode(',', array_keys($suppliers));

// 获取直销产品
$pids_arr = array();
$sql = "select * from pft_product_sale_list where fid=$memberSID and status=0 and aid in ($suppliers_str)";
$GLOBALS['le']->query($sql);
while($row=$GLOBALS['le']->fetch_assoc()){
	if($row['pids']=='A'){
		$saleAll_aid[$row['aid']] = $row['aid']; continue;
	}
	$pids_arr = array_merge($pids_arr, explode(',', $row['pids']));
}


$sale_f = array('3385', '6970', '6864', '6874', '6889', '26816', '7136', '2148');
$pay = 't.pay=1';
if(in_array($_SESSION['sid'],$sale_f)) $pay ='1=1';

$pids_arr = array_filter($pids_arr);
$pids_arr = array_unique($pids_arr);
if(count($pids_arr)){
	$pids_str = implode(',', $pids_arr);
	$sql = "select p.id as pid,p.apply_did,t.landid,t.title as ttitle,t.id as tid from uu_products p left join uu_jq_ticket t on t.pid=p.id where p.apply_limit=1 and p.p_status in (0,2,3,4,5) and $pay and p.apply_did<>$memberSID and p.id in ($pids_str)";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$p_key = $row['pid'].'_'.$row['apply_did'];
		$prods[$p_key] = formatEvoData($row, $memberSID);
		$lids[] = $row['landid'];
	}
}

$saleAll_aid = array_intersect_key($saleAll_aid, $suppliers);
if(count($saleAll_aid)){
	$sql = "select p.id as pid,p.apply_did,t.landid,t.title as ttitle,t.id as tid from uu_products p left join uu_jq_ticket t on t.pid=p.id where p.apply_limit=1 and p.p_status in (0,2,3,4,5) and $pay and p.apply_did<>$memberSID and p.apply_did in (".implode(',', $saleAll_aid).")";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$p_key = $row['pid'].'_'.$row['apply_did'];
		$prods[$p_key] = formatEvoData($row, $memberSID);
		$lids[] = $row['landid'];
	}
}

// 获取已经在 pft_p_apply_evolute 设置的直销记录 即 lvl=1
$sql = "select e.id,e.fid,e.pid,e.sid,e.sourceid,e.lvl,e.status,e.aids,e.active,t.landid,t.title as ttitle,t.id as tid ";
$sql.= "from pft_p_apply_evolute e inner join uu_products p on p.id=e.pid left join uu_jq_ticket t on t.pid=e.pid ";
$sql.= "where fid=$memberSID and p.apply_limit=1 and p.p_status in(0,2,3,4,5) and e.status=0 and sourceid<>$memberSID and $pay and e.lvl=1";
$GLOBALS['le']->query($sql);
while($row=$GLOBALS['le']->fetch_assoc()){
	$p_key = $row['pid'].'_'.$row['sid'];
	if(!isset($prods[$p_key])) continue;
	$prods[$p_key] = $row;
	$lids[] = $row['landid'];
}

// 获取转分销产品
$sql = "select e.id,e.fid,e.pid,e.sid,e.sourceid,e.lvl,e.status,e.aids,e.active,t.landid,t.title as ttitle,t.id as tid ";
$sql.= "from pft_p_apply_evolute e inner join uu_products p on p.id=e.pid left join uu_jq_ticket t on t.pid=e.pid ";
$sql.= "where fid=$memberSID and p.apply_limit=1 and p.p_status in(0,2,3,4,5) and e.status=0 and sourceid<>$memberSID and $pay and e.lvl>1 and sid in ($suppliers_str)";
$GLOBALS['le']->query($sql);
while($row=$GLOBALS['le']->fetch_assoc()){
	$p_key = $row['pid'].'_'.$row['sid'];
	$prods[$p_key] = $row;
	$lids[] = $row['landid'];
}


// 获取景区
if(count($lids)){
    $lids_str = implode(',', array_unique($lids));
    $sql_dname = "select id,title,area from uu_land where id in ($lids_str) limit ".count($lids);
    $GLOBALS['le']->query($sql_dname);
    while($row = $GLOBALS['le']->fetch_assoc()) {
        $lands[$row['id']] = $row;
    }
}

// 景区模糊查询
$lids = array();
if(isset($_REQUEST['title']) && (!isset($_REQUEST['search_type']) || $_REQUEST['search_type']==0)){
	$ltitle = $_REQUEST['title'];
	$sql = "select id from uu_land where title like '%$ltitle%'";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()) $lids[] = $row['id'];
}
$lids_count = count($lids);// 模糊查询出来的景区ID


// echo count($prods),',',microtime(true) - $start;
// print_r($lands);
// print_r($prods);
// exit;

// 分页依据
$pageSize = isset($_REQUEST['pageSize']) ? $_REQUEST['pageSize']+0:5;
$lastRows = isset($_REQUEST['last']) ? $_REQUEST['last']:'';// lid 
$provice  = isset($_REQUEST['provice']) ? $_REQUEST['provice']:'';
$city     = isset($_REQUEST['city']) ? $_REQUEST['city']:'';
$pageSize = 5;
//  5 个景区一组

$buoy = 0;// 数据浮标
$count = 0;
$today = date('Y-m-d'); 
$lastRow = '';
$run_f = ($lastRows=='') ? true:false;
$prods_land = array();
foreach($prods as $row) $prods_land[$row['landid']][] = $row;
if($_REQUEST['d']==1) print_r($prods_land);
foreach($prods_land as $landid=>$prods_row){
	
	if($lids_count && !in_array($landid, $lids)) continue;
	
	
	if($lastRow!='') break;
	if($lastRows && $run_f==false){// 第二页及以上
		if($lastRows==$landid){
			$lastRows = '';
			$run_f = true;
			continue;
		}
	}
	
	if($run_f==false) continue;
	
	foreach($prods_row as $prod){
		
		$areas_split = explode('|', $lands[$prod['landid']]['area']);// 拆分地区

		if($provice && !in_array($provice, $areas_split)) continue;
		if($city && !in_array($city, $areas_split)) continue;
		
		
		$price = abc777\Product::Price($s, $prod['pid'], $prod['sid'], $today);
		if($price['js']==-1) continue;// 没有价格

		if(!isset($sales[$prod['landid']])){// 景区信息	
			$land['title'] = $lands[$prod['landid']]['title'];
			$land['lid']   = $prod['landid'];
			$land['memberSID'] = $_SESSION['sid'];
			$land['province'] = $areas[$areas_split[0]];
			$land['city'] = $areas[$areas_split[1]];
			$land['areas']      = $areas_split;
			$sales[$prod['landid']] = $land;
			$total = $total + 1;
		}
		
		$ticket['aid'] = $prod['sid'];
		$ticket['lvl'] = $prod['lvl'];
		$ticket['active'] = $prod['active'];
		$ticket['js'] = $price['js'];
		$ticket['ls'] = $price['ls'];
		$ticket['dname'] = $suppliers_t[$prod['sid']];
		$ticket['conf_price'] = 1;// 前端是否显示配置分销价格按钮条件 1 配置
		$ticket['sid'] = $prod['sid'];
		$ticket['aids'] = $prod['aids'];
		

		if($prod['lvl']>=4 || (isset($_SESSION['alliance']) && $_SESSION['alliance']!='')) $ticket['conf_price'] = 0;
		
		
		$sales[$prod['landid']]['ticket'][$prod['tid']]['title'] = $prod['ttitle'];
		$sales[$prod['landid']]['ticket'][$prod['tid']]['pid'] = $prod['pid'];
		$sales[$prod['landid']]['ticket'][$prod['tid']]['lists'][] =  $ticket;
		
	}
	
	if($total>=$pageSize){
		$lastRow = $prod['landid']; break;
	}
}

/*
foreach($prods as $key=>$prod){

	if($lastRow!='' && $lastRow!=$prod['landid']) continue;
	
	if($lastRows && $run_f==false){// 第二页及以上
		if($lastRows==$prod['landid']){
			$lastRows = '';
			$run_f = true;
			continue;
		}
	}
	if($run_f==false) continue;
	
	$price = abc777\Product::Price($s, $prod['pid'], $prod['sid'], $today);
	if($price['js']==-1) continue;// 没有价格

	if(!isset($sales[$prod['landid']])){// 景区信息	
		$land['title'] = $lands[$prod['landid']]['title'];
		$land['lid']   = $prod['landid'];
		$areas_split = explode('|', $lands[$prod['landid']]['area']);// 拆分地区
		$land['province'] = $areas[$areas_split[0]];
		$land['city'] = $areas[$areas_split[1]];
		$land['areas']      = $areas_split;
		$sales[$prod['landid']] = $land;
		$total = $total + 1;
	}
	
	$ticket['aid'] = $prod['sid'];
	$ticket['lvl'] = $prod['lvl'];
	$ticket['active'] = $prod['active'];
	$ticket['js'] = $price['js'];
	$ticket['ls'] = $price['ls'];
	$ticket['dname'] = $suppliers[$prod['sid']];
	
	$sales[$prod['landid']]['ticket'][$prod['tid']]['title'] = $prod['ttitle'];
	$sales[$prod['landid']]['ticket'][$prod['tid']]['pid'] = $prod['pid'];
	$sales[$prod['landid']]['ticket'][$prod['tid']]['lists'][] =  $ticket;
	if($total>=$pageSize) $lastRow = $prod['landid'];
	
}
*/

// 没有转分销产品
if(count($sales)==0) exit(json_encode(array('lists' => array(), 'total' => 0, 'code'=>200)));
$response = array(
	'lists' => $sales,
	'total' => $total,
	'code'  => 200,
	'last'  => $lastRow
);
echo json_encode($response);



function formatEvoData($params, $memberSID){
	$params['fid']      = $memberSID;
	$params['sid']      = $params['apply_did'];
	$params['lvl']      = 0;
	$params['aids']     = $params['apply_did'];
	$params['status']   = 0;
	$params['active']   = 0;
	$params['sourceid'] = $params['apply_did'];
	return $params;
}
